﻿using TodoList.Data;

namespace TodoList.MinimalApi.Endpoints.TodoList;

public class Delete : IEndpoint
{
	public void MapEndpoint(IEndpointRouteBuilder endpoints)
	{
		endpoints.MapDelete("/api/todolist/{id:int}", async (ApplicationDbContext db,int id) =>
		{
			var todo = await db.TodoItems.FindAsync(id);
			if (todo == null)
			{
				return Results.NotFound("Todo item not found");
			}
			db.TodoItems.Remove(todo);
			await db.SaveChangesAsync();
			return Results.Ok("Todo item deleted successfully");
		})
		.WithName("DeleteTodoItem")
		.WithTags(Tags.TodoList);
	}
}
